
package cn.live.kyle.questionnaire.question;

import java.util.ArrayList;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import cn.live.kyle.questionnaire.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class QuestionCheckPage {

    private NodeList mPageList;
    private ViewGroup mContainer;

    private Context mContext;

    private View mPageView;

    private OnQuestionClicked mOnQuestionClicked;

    private CheckListAdapter mCheckListAdapter;

    private int mStart;
    private int mCount;

    public interface OnQuestionClicked {
        void onClick(int pageIndex);
    }

    public QuestionCheckPage(NodeList pageList, int start, int count, ViewGroup container) {
        mStart = start;
        mCount = count;

        mPageList = pageList;
        mContainer = container;

        mContext = container.getContext();
    }

    public QuestionCheckPage(NodeList pageList, ViewGroup container) {
        this(pageList, 0, pageList.getLength(), container);
    }

    public void setOnQuestionClicked(OnQuestionClicked l) {
        mOnQuestionClicked = l;
    }

    public View getCheckPageView() {
        mPageView = LayoutInflater.from(mContext).inflate(
                R.layout.question_page_check, mContainer, false);
        ListView listView = (ListView) mPageView.findViewById(R.id.checkList);

        ArrayList<QuestionItemData> itemList = getQuestionItemList();
        mCheckListAdapter = new CheckListAdapter(itemList);
        listView.setAdapter(mCheckListAdapter);
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view,
                    int position, long id) {
                if (mOnQuestionClicked != null) {
                    QuestionItemData itemData = mCheckListAdapter.mQuestionItemList
                            .get(position);
                    mOnQuestionClicked.onClick(itemData.pageIndex);
                }
            }
        });

        return mPageView;
    }

    private ArrayList<QuestionItemData> getQuestionItemList() {
        ArrayList<QuestionItemData> itemList = new ArrayList<QuestionItemData>();
        // int pageCount = mPageList.getLength();
        for (int i = mStart; i < mStart + mCount; i++) {
            Element page = (Element) mPageList.item(i);
            NodeList questionList = page
                    .getElementsByTagName(QuestionDomParser.TAG_QUESTION);

            int questionCount = questionList.getLength();
            for (int j = 0; j < questionCount; j++) {
                QuestionItemData questionItem = new QuestionItemData();
                questionItem.questionData = (Element) questionList.item(j);
                questionItem.pageIndex = i;
                itemList.add(questionItem);
            }
        }
        return itemList;
    }

    private class QuestionItemData {
        Element questionData;
        int pageIndex;
    }

    private class CheckListAdapter extends BaseAdapter {

        protected ArrayList<QuestionItemData> mQuestionItemList;

        public CheckListAdapter(ArrayList<QuestionItemData> questionItemList) {
            mQuestionItemList = questionItemList;
        }

        @Override
        public int getCount() {
            return mQuestionItemList.size();
        }

        @Override
        public Object getItem(int position) {
            return mQuestionItemList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = convertView;
            if (itemView == null) {
                itemView = LayoutInflater.from(mContext).inflate(
                        R.layout.question_page_check_item, parent, false);
            }

            TextView titleView = (TextView) itemView
                    .findViewById(R.id.itemTitle);
            TextView answerView = (TextView) itemView
                    .findViewById(R.id.itemAnswer);

            Element element = mQuestionItemList.get(position).questionData;
            String questionType = element
                    .getAttribute(QuestionDomParser.ATTR_TYPE);
            if (questionType.equals(QuestionDomParser.ATTR_TYPE_RADIO)) {
                String title = element
                        .getAttribute(QuestionDomParser.ATTR_TITLE);
                String answer = element
                        .getAttribute(QuestionDomParser.ATTR_ANSWER);
                try {
                    int index = Integer.valueOf(answer);

                    NodeList optionList = element
                            .getElementsByTagName(QuestionDomParser.TAG_OPTION);
                    Element option = (Element) optionList.item(index);
                    answer = option.getAttribute(QuestionDomParser.ATTR_TITLE);
                } catch (Exception e) {
                    answer = "";
                }

                titleView.setText(title);
                answerView.setText(answer);
            } else if (questionType
                    .equals(QuestionDomParser.ATTR_TYPE_MULTISELECT)) {
                String title = element
                        .getAttribute(QuestionDomParser.ATTR_TITLE);
                String answer = element
                        .getAttribute(QuestionDomParser.ATTR_ANSWER);
                try {
                    int answerInt = Integer.valueOf(answer);
                    NodeList optionList = element
                            .getElementsByTagName(QuestionDomParser.TAG_OPTION);
                    answer = "";
                    int count = 0;
                    for (int i = 0; i < optionList.getLength(); i++) {
                        if (QuestionMultiselect.getOptionStateFromAnswer(answerInt,
                                i)) {
                            Element option = (Element) optionList.item(i);
                            if (count != 0) {
                                answer += "\n";
                            }
                            answer += i
                                    + option.getAttribute(QuestionDomParser.ATTR_TITLE);
                            count++;
                        }
                    }
                } catch (Exception e) {
                    answer = "";
                }

                titleView.setText(title);
                answerView.setText(answer);
            } else {
                String title = element
                        .getAttribute(QuestionDomParser.ATTR_TITLE);
                String answer = element
                        .getAttribute(QuestionDomParser.ATTR_ANSWER);

                titleView.setText(title);
                answerView.setText(answer);
            }

            return itemView;
        }
    }

    public void setPrePageButtonListener(View.OnClickListener listener) {
        View button = mPageView.findViewById(R.id.prePage);
        button.setOnClickListener(listener);
    }

    public void setNextPageButtonListener(View.OnClickListener listener) {
        View button = mPageView.findViewById(R.id.nextPage);
        button.setOnClickListener(listener);
    }

    public void setNextPageButtonTitle(int title) {
        TextView button = (TextView) mPageView.findViewById(R.id.nextPage);
        button.setText(title);
    }
}
